using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Appr;
using net_work.Data.Enum.Appr;

namespace net_work.Config.DataBaseConfig.Appr;

/// <summary>
/// 流程节点实体配置类
/// </summary>
public class FlowNodeEntityTypeConfiguration : IEntityTypeConfiguration<FlowNode>
{
    public void Configure(EntityTypeBuilder<FlowNode> builder)
    {
        builder.ToTable("appr_flow_node");

        builder.HasKey(fn => fn.Id);

        // 配置字段名映射
        builder.Property(fn => fn.Id).HasColumnName("flow_node_id");
        builder.Property(fn => fn.Name).HasColumnName("flow_node_name").IsRequired().HasMaxLength(64);

        // 配置枚举转换为字符串
        builder.Property(fn => fn.ProcessingType)
            .HasColumnName("flow_node_pcs_type")
            .IsRequired()
            .HasMaxLength(64)
            .HasConversion(
                v => v.ToString(),
                v => (NodeProcessingType)Enum.Parse(typeof(NodeProcessingType), v)
            );

        builder.Property(fn => fn.ProcessingId).HasColumnName("flow_node_pcs_id").IsRequired();

        builder.Property(fn => fn.SuccessType)
            .HasColumnName("flow_node_scs_type")
            .IsRequired()
            .HasMaxLength(64)
            .HasConversion(
                v => v.ToString(),
                v => (NodeSuccessType)Enum.Parse(typeof(NodeSuccessType), v)
            );

        builder.Property(fn => fn.SuccessConfig).HasColumnName("flow_node_scs_cfg");
        builder.Property(fn => fn.SuccessNodeId).HasColumnName("flow_node_scs_id");
        builder.Property(fn => fn.FailureNodeId).HasColumnName("flow_node_fal_id");
        builder.Property(fn => fn.FlowId).HasColumnName("flow_id").IsRequired();

        builder.Property(fn => fn.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(fn => fn.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(fn => fn.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(fn => fn.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(fn => fn.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(fn => fn.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}